package cn.com.taiji.oospub.wechat.common;



import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @ClassName: JsonDateValueProcessor
 * @Description: JSON 日期格式处理（java转化为JSON）
 * @author sunwei Email：sunwei@mail.taiji.com.cn
 * @date 2012-8-6 - 下午12:11:40
 * @version : 1.0
 */
public class JsonDateValueProcessor implements JsonValueProcessor { 

    /** 
     * datePattern 
     */ 
    private String datePattern = "yyyy-MM-dd HH:mm:ss"; 

    /** 
     * JsonDateValueProcessor 
     */ 
    public JsonDateValueProcessor() { 
        super(); 
    } 

    /** 
     * @param format 
     */ 
    public JsonDateValueProcessor(String format) { 
        super(); 
        this.datePattern = format; 
    } 

    /** 
     * @param value 
     * @param jsonConfig 
     * @return Object 
     */ 
    @Override
	public Object processArrayValue(Object value, JsonConfig jsonConfig) { 
        return process(value); 
    } 

    /** 
     * @param key 
     * @param value 
     * @param jsonConfig 
     * @return Object 
     */ 
    @Override
	public Object processObjectValue(String key, Object value, 
            JsonConfig jsonConfig) { 
        return process(value); 
    } 

    /** 
     * process 
     * @param value 
     * @return 
     */ 
    private Object process(Object value) { 
        try { 
            if (value instanceof Date) { 
                SimpleDateFormat sdf = new SimpleDateFormat(datePattern); 
                return sdf.format((Date) value); 
            } 
            return value == null ? "" : value.toString(); 
        } catch (Exception e) { 
            return ""; 
        } 

    } 

    /** 
     * @return the datePattern 
     */ 
    public String getDatePattern() { 
        return datePattern; 
    } 

    /** 
     * @param pDatePattern the datePattern to set 
     */ 
    public void setDatePattern(String pDatePattern) { 
        datePattern = pDatePattern; 
    }

} 

